﻿@model ProductModel

@using Telerik.Web.Mvc.UI;

@if (Model.Id > 0)
{
    if (Model.NumberOfAvailableCategories > 0)
    {
        <script>
			function onProductCategoryEdit(e) {
				if (e.mode == "edit") {
					$('#Category')
						.data('select-selected-id', e.dataItem['CategoryId'])
						.data('select-init-text', e.dataItem['Category'])
						.val(e.dataItem['CategoryId'])
						.trigger('change');
				}
			}

			function onProductCategoryDataBound(e) {
				$('#Category').selectWrapper({ resetDataUrl: '@Url.Action("AllCategories", "Category")' });
			}
        </script>
		
		@(Html.Telerik().Grid<ProductModel.ProductCategoryModel>()
			.Name("productcategories-grid")
			.DataKeys(keys =>
			{
				keys.Add(x => x.Id);
			})
			.DataBinding(dataBinding =>
			{
				dataBinding.Ajax()
					.Select("ProductCategoryList", "Product", new { productId = Model.Id })
					.Insert("ProductCategoryInsert", "Product", new { productId = Model.Id })
					.Update("ProductCategoryUpdate", "Product")
					.Delete("ProductCategoryDelete", "Product");
			})
			.Columns(columns =>
			{
				columns.Bound(x => x.Category)
					.Width("60%")
					.Template(x => Html.ActionLink(x.Category, "Edit", "Category", new { id = x.CategoryId }))
					.ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Category/Edit/") + "<#= CategoryId #>\"><#= Category #></a>");
				columns.Bound(x => x.IsFeaturedProduct)
					.Width(100)
					.Template(item => @Html.SymbolForBool(item.IsFeaturedProduct))
					.ClientTemplate(@Html.SymbolForBool("IsFeaturedProduct"))
					.Centered();
				columns.Bound(x => x.DisplayOrder)
					.Centered()
					.Width(100);
				columns.Command(commands =>
				{
					commands.Edit().Localize(T);
					commands.Delete().Localize(T);
				})
				.HtmlAttributes(new { align = "right" });
			})
			.ToolBar(commands => commands.Insert())
			.ClientEvents(events => {
				events.OnEdit("onProductCategoryEdit");
				events.OnDataBound("onProductCategoryDataBound");
			})
			.EnableCustomBinding(true))
    }
    else
    {
        <div class="alert alert-danger">
            @T("Admin.Catalog.Products.Categories.NoCategoriesAvailable")
        </div>
    }
}
else
{
    <div class="alert alert-danger">
        @T("Admin.Catalog.Products.Categories.SaveBeforeEdit")
    </div>
}